ExposedMembers.SIV = ExposedMembers.SIV or {};
ExposedMembers.SIV.Utils = ExposedMembers.SIV.Utils or {};
SIVUtils = ExposedMembers.SIV.Utils;

-- 切换政体
local StefanBatory_Government_Tag = 'SIV_StefanBatory_Government_';
function StefanBatoryGovernmentChanged(playerId, governmentId)
	if governmentId ~= nil and governmentId ~= -1 and SIVUtils.LeaderHasTrait(playerId, 'TRAIT_LEADER_SIV_STEFANBATORY') then
    local player = Players[playerId];
    if player and player:GetProperty(StefanBatory_Government_Tag .. governmentId) ~= 1 then
      player:SetProperty(StefanBatory_Government_Tag .. governmentId, 1);
      player:AttachModifierByID('SIV_STEFANBATORY_HEAVY_CAVALRY_SET_PROPERTY');
      print("武波兰 切换政体 重骑兵加力", playerId, governmentId)
    end
  end
end
Events.GovernmentChanged.Add(StefanBatoryGovernmentChanged)

-- 切换政策
local SIV_STEFANBATORY_MILITARY_POLICY_NUM = GlobalParameters.SIV_STEFANBATORY_MILITARY_POLICY_NUM or 0;
local SIV_STEFANBATORY_DIPLOMATIC_POLICY_NUM = GlobalParameters.SIV_STEFANBATORY_DIPLOMATIC_POLICY_NUM or 0;
local SIV_STEFANBATORY_MILITARY_POLICY_TAG = 'SIV_STEFANBATORY_MILITARY_POLICY_'
local SIV_STEFANBATORY_DIPLOMATIC_POLICY_TAG = 'SIV_STEFANBATORY_DIPLOMATIC_POLICY_'
local SIV_STEFANBATORY_MILITARY_POLICY_NUM_TAG = 'SIV_STEFANBATORY_MILITARY_POLICY_NUM'
local SIV_STEFANBATORY_DIPLOMATIC_POLICY_NUM_TAG = 'SIV_STEFANBATORY_DIPLOMATIC_POLICY_NUM'
function StefanBatoryOnPolicyChanged(playerId, policyId)
  if SIVUtils.LeaderHasTrait(playerId, 'TRAIT_LEADER_SIV_STEFANBATORY') then
    local player = Players[playerId];
    local playerCulture = player:GetCulture()
    local numSlots = playerCulture:GetNumPolicySlots();
    for i = 0, numSlots-1, 1 do
      local	policyId = playerCulture:GetSlotPolicy(i);
      if policyId ~= nil and policyId ~= -1 then
        local policy = GameInfo.Policies[policyId]
        if SIV_STEFANBATORY_MILITARY_POLICY_NUM > 0 and policy.GovernmentSlotType == 'SLOT_MILITARY' then
          if player:GetProperty(SIV_STEFANBATORY_MILITARY_POLICY_TAG .. policy.PolicyType) ~= 1 then
            player:SetProperty(SIV_STEFANBATORY_MILITARY_POLICY_TAG .. policy.PolicyType, 1)
            print('武波兰 军事政策', Locale.Lookup(policy.Name), policy.GovernmentSlotType)
            
            local num = player:GetProperty(SIV_STEFANBATORY_MILITARY_POLICY_NUM_TAG) or 0;
            print('武波兰 军事政策 计数', num + 1)
            if num < SIV_STEFANBATORY_MILITARY_POLICY_NUM - 1 then
              player:SetProperty(SIV_STEFANBATORY_MILITARY_POLICY_NUM_TAG, num + 1);
            else
              player:SetProperty(SIV_STEFANBATORY_MILITARY_POLICY_NUM_TAG, 0);
              for row in GameInfo.StefanBatory_Military_Policy_Modifiers() do
                player:AttachModifierByID(row.ModifierId)
                print('武波兰 军事政策 建筑增产', row.ModifierId)
              end
            end
          end
        elseif SIV_STEFANBATORY_DIPLOMATIC_POLICY_NUM > 0 and policy.GovernmentSlotType == 'SLOT_DIPLOMATIC' then
          if player:GetProperty(SIV_STEFANBATORY_DIPLOMATIC_POLICY_TAG .. policy.PolicyType) ~= 1 then
            player:SetProperty(SIV_STEFANBATORY_DIPLOMATIC_POLICY_TAG .. policy.PolicyType, 1)
            print('武波兰 文化政策', Locale.Lookup(policy.Name), policy.GovernmentSlotType)
            
            local num = player:GetProperty(SIV_STEFANBATORY_DIPLOMATIC_POLICY_NUM_TAG) or 0;
            print('武波兰 文化政策 计数', num + 1)
            if num < SIV_STEFANBATORY_DIPLOMATIC_POLICY_NUM - 1 then
              player:SetProperty(SIV_STEFANBATORY_DIPLOMATIC_POLICY_NUM_TAG, num + 1);
            else
              player:SetProperty(SIV_STEFANBATORY_DIPLOMATIC_POLICY_NUM_TAG, 0);
              for row in GameInfo.StefanBatory_Diplomatic_Policy_Modifiers() do
                player:AttachModifierByID(row.ModifierId)
                print('武波兰 文化政策 建筑增产', row.ModifierId)
              end
            end
          end
        end
      end
    end
  end
end
Events.GovernmentPolicyChanged.Add(StefanBatoryOnPolicyChanged);